package com.sqy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sqy.entity.Goods;
import com.sqy.exception.ImoocMallExceptionEnum;
import com.sqy.service.GoodsService;
import com.sqy.utlis.ApiResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Goods")
@Api(tags = "商品对象")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @PostMapping("/add")
    public ApiResult AddGoods(@RequestBody Goods goods){

        boolean save = goodsService.save(goods);
        if(save){
            return  ApiResult.success();
        }
        return  ApiResult.error(ImoocMallExceptionEnum.ADD_FAIL);
    }


    @GetMapping("/list")
    public ApiResult  getGoodsList(
            @RequestParam("name") String name,
            @RequestParam("id") Integer id){
        System.out.println(id);

        if(id==1){
            LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();

            wrapper.like(name != null,Goods::getName,name);
            wrapper.orderByAsc(Goods::getPrice);
            List<Goods> list = goodsService.list(wrapper);

            return  ApiResult.success(list);
        }
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(name != null,Goods::getName,name);
        wrapper.orderByDesc(Goods::getPrice);
        List<Goods> list = goodsService.list(wrapper);

        return  ApiResult.success(list);

    }



}
